package dp.mobile.level;

import java.util.ArrayList;

import org.w3c.dom.Node;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import dp.mobile.glwrapper.GLTexture;
import dp.mobile.glwrapper.GameRenderer;
import dp.mobile.level.CutScene.MiniDialog;
import dp.mobile.system.Utilities;

public class Dialog {
	protected Dialog() {
		//Initialize
		mWave		= -1;
		mCurrent	= 0;
		mPositions	= new ArrayList<Integer>();
		mDialog	= new ArrayList<ArrayList<MiniDialog>>();
	}

	public Dialog(Node root) {
		//Default
		this();
		
		//Get wave
		if (root.getAttributes().getNamedItem("wave") != null)
			mWave = Integer.parseInt(root.getAttributes().getNamedItem("wave").getNodeValue());

		//Get character image path
		if (root.getAttributes().getNamedItem("image") != null)
			mCharacterImgPath = root.getAttributes().getNamedItem("image").getNodeValue();

		//For each children
		int childCount = root.getChildNodes().getLength();
		for (int i = 0; i < childCount; i++) {
			//Get child
			Node child = root.getChildNodes().item(i);
			if (child.getNodeType() == Node.TEXT_NODE) continue;

			//Is it dialog?
			if (child.getNodeName().equals("dialog")) {
				//Get position
				mPositions.add(new Integer(Integer.parseInt(child.getAttributes().getNamedItem("position").getNodeValue())));

				//Create vector
				ArrayList<MiniDialog> Lines = new ArrayList<MiniDialog>();

				//For each child
				int grandChildCount = child.getChildNodes().getLength();
				for (int j = 0; j < grandChildCount; j++) {
					//If line
					Node GrandChild = child.getChildNodes().item(j);
					if (GrandChild.getNodeName().equals("line")) {
						//Insert to vector
						Lines.add(new MiniDialog(GrandChild));
					}
				}

				//If not empty
				if (!Lines.isEmpty()) mDialog.add(Lines);
			}
		}
	}

	public int getWave() {
		return mWave;
	}

	public Bitmap getCharacterImg(){
		return mCharacterImg;
	}

	public void loadCharacterImg(){
		if(mCharacterImgPath != null)
			mCharacterImg = BitmapFactory.decodeFile(Utilities.INTERFACE_FOLDER + "/" + mCharacterImgPath + ".png");
	}

	public void unloadCharacterImg(){
		mCharacterImg.recycle();
		mCharacterImg = null;
	}
	
	public ArrayList<ArrayList<MiniDialog>> getDialogs() {
		return mDialog;
	}

	public MiniDialog[] nextDialog() {
		//Variable
		MiniDialog[] Lines = null;

		//if current dialog is valid
		if (mCurrent >= 0 && mCurrent < mDialog.size()) {
			//Get vector
			ArrayList<MiniDialog> LineVector = mDialog.get(mCurrent);
			Lines = new MiniDialog[LineVector.size()];

			//Copy to array
			for (int i = 0; i < Lines.length; i++) 
				Lines[i] = LineVector.get(i);

			//Next
			mCurrent++;
		}

		//Return
		return Lines;
	}

	public int getPosition() {
		//Variable
		int Position = 0;

		//if current dialog is valid
		if (mCurrent >= 0 && mCurrent < mDialog.size()) {
			//Get positiom
			Position = mPositions.get(mCurrent);
		}

		//Return
		return Position;
	}

	//Members
	protected int		mWave;
	protected int		mCurrent;
	protected String	mCharacterImgPath;
	protected Bitmap	mCharacterImg;

	protected ArrayList<Integer>	mPositions;
	protected ArrayList<ArrayList<MiniDialog>>	mDialog;
}
